package movieexample.movieexample.controller;

import movieexample.movieexample.pojo.CustomLink;
import movieexample.movieexample.pojo.CustomNode;
import movieexample.movieexample.pojo.Movie;
import movieexample.movieexample.pojo.Person;
import movieexample.movieexample.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/person")
public class PersonController {
    @Autowired
    PersonService personService;
    /*
    * 根绝人名 查询详细信息
    * */
    @GetMapping("/{name}")
    public List<CustomNode> findByName(@PathVariable("name") String name){
        Person person=personService.findByName(name);
        List<CustomNode> nodes=new ArrayList<>();
        int group=(int)(0+Math.random()*10);
        nodes.add(new CustomNode(person.getName(),group));
        return nodes;
    }
    /*
    *返回和人名有关的电影节点
     */
    @GetMapping("/actedby/{name}")
    public List<CustomNode> getActedByName(@PathVariable("name") String name){

        Person person=personService.findByName(name);
        Set<Movie> movieSet=person.getActedInMovie();
        List<CustomNode> customNodes=new ArrayList<>();
        for (Movie movie:movieSet){
            int group=(int)(0+Math.random()*10);
            customNodes.add(new CustomNode(movie.getTitle(),group));
        }
        return customNodes;
    }
    /*
    * 测试
    * */
    @GetMapping("/test")
    public String test(){
        return "hello";
    }
    /*
    * 查询所有人信息
    * */
    @GetMapping("/all")
    public List<CustomNode> findAll(){
        Iterable<Person> presons=personService.findAll();
        List<CustomNode> nodes=new ArrayList<>();
        for (Person person:presons) {
            int group=(int)(0+Math.random()*10);
            nodes.add( new CustomNode(person.getName(),group));
        }
        return nodes;
    }
}
